package com.cooldatasoft.currency.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

import com.cooldatasoft.currency.data.ExchangeRate;

@Slf4j
public class RateHelper {

	public static List<String> failList = new ArrayList<String>(); 
	
	private final static ExecutorService executorService = Executors.newFixedThreadPool(50);
	private final static RateHelper RATE_HELPER = new RateHelper();
	
	private RateHelper(){}
	 
	public static RateHelper get(){
		return RATE_HELPER;
	}
	
	public ExchangeRate getRate(String from, String to) {
		GetRateCallable getRateCallable = new GetRateCallable(from,to);
		Future<ExchangeRate> future = executorService.submit(getRateCallable);
		try {
			return future.get(5, TimeUnit.SECONDS);
		} catch (Exception e) {
			failList.add(to);
			log.error("FAILED from {} to {} !",from,to);
//			e.printStackTrace();
			return null;
		}
	}
}

